package com.rzrk.centerlogserver.action;

import java.beans.PropertyDescriptor;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.exp.base.action.EasyuiPage;
import com.exp.base.action.EasyuiPageInfo;
import com.exp.base.action.Message;
import com.exp.base.util.BeanUtils;
import com.exp.base.util.DateUtils;
import com.exp.base.util.ExceptionUtils;
import com.exp.base.util.JsonUtils;
import com.rzrk.centerlog.entity.NormalLog;
import com.rzrk.centerlogserver.service.NormalLogService;

@Controller
@RequestMapping("normalLog")
public class NormalLogAction extends BaseAction {
	@Resource
	NormalLogService normalLogService;
	
	@RequestMapping("list")
	public String list(){
		
		return "normal_log/normal_log_list";
	}
	
	@ResponseBody
	@RequestMapping("query")
	public Message query(String queryStr,EasyuiPage easyuiPage){
		Pageable pageable = new PageRequest(easyuiPage.getPage()-1, easyuiPage.getRows());
		Query query = new Query();
		Criteria criteriaAll = new Criteria();
		List<Criteria> criteriaCreateDateList = new ArrayList<Criteria>(); 
		if(StringUtils.isNoneBlank(queryStr)){
			Map<String,Object> map = JsonUtils.toObject(queryStr, Map.class);
			for(Entry<String, Object> entry : map.entrySet()){
				String value = (String)entry.getValue();
				if(StringUtils.isNotBlank(value)){
					String key = entry.getKey();
					if(StringUtils.equalsIgnoreCase(key, "level")){
						int level = NumberUtils.toInt(value, NormalLog.LEVEL_DEBUG);
						criteriaAll.and(key).gte(level); 
					}else if(StringUtils.equalsIgnoreCase(key, "createDateBegin")){
						try {
							Date createDateBegin = DateUtils.parseDate(value);
							Criteria criteria = Criteria.where("createDate").gte(createDateBegin); 
							criteriaCreateDateList.add(criteria)	;
						} catch (ParseException e) {
							ExceptionUtils.throwBusinessException("开始时间错误", e);
						}
					}else if(StringUtils.equalsIgnoreCase(key, "createDateEnd")){
						try {
							Date createDateEnd = DateUtils.parseDate(value);
							Criteria criteria = Criteria.where("createDate").lte(createDateEnd); 
							criteriaCreateDateList.add(criteria)	;
						} catch (ParseException e) {
							ExceptionUtils.throwBusinessException("开始时间错误", e);
						}
					}else if(StringUtils.equalsIgnoreCase(key, "contentText")){
						criteriaAll.and(key).regex(".*"+entry.getValue()+".*"); 
					}else{
						criteriaAll.and(key).is(entry.getValue()); 
					}
				}
			}
		}
		if(!criteriaCreateDateList.isEmpty()){
			criteriaAll.andOperator(criteriaCreateDateList.toArray(new Criteria[criteriaCreateDateList.size()]));
		}
		query.addCriteria(criteriaAll);
		String orderStr = easyuiPage.getOrder();
		if(StringUtils.isNotBlank(orderStr)){
			String sortStr = easyuiPage.getSort();
			Sort sort = new Sort(Direction.fromStringOrNull(orderStr),sortStr);
			query.with(sort);
		}
		
//		String queryDbStr = query.getQueryObject().toString();
//		System.out.println(queryDbStr);
		
		Page<NormalLog> pageInfo = normalLogService.query(query,pageable);
		EasyuiPageInfo easyuiPageInfo = new EasyuiPageInfo();
		easyuiPageInfo.setTotal(pageInfo.getTotalElements());
		easyuiPageInfo.addRows(pageInfo.getContent());
		return new Message(true, easyuiPageInfo);
	}
	
	@RequestMapping("realtime")
	public String realtime(){
		return "normal_log/normal_log_realtime";
	}
	

	
}
